<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="部门/公司名称">
                <a-input v-model="queryParam.search" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" icon="plus" @click="toAdd">新增</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table
        ref="table"
        :columns="columns"
        :dataSource="departmentList"
        :row-key="(record)=>record.id"
        :loading="loading"
        :showPagination="true"
        @expand="iconExpand"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="toSetting(record)">配置</a>
            <a-divider type="vertical" />
            <a @click="addChildren(record)">添加子部门</a>
            <a-divider type="vertical" />
            <a @click="toDetail(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm placement="topLeft" ok-text="是" cancel-text="否" @confirm="toDelete(record)">
              <template slot="title">
                <p>确定要删除这条记录吗？</p>
              </template>
              <!-- <a-button>TL</a-button> -->
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </a-table>
      <!-- <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :row-key="(record)=>record.id"
        :loading="loading"
        :showPagination="true"
        @expand="iconExpand"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="addChildren(record)">添加子部门</a>
            <a-divider type="vertical" />
            <a @click="toDetail(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm placement="topLeft" ok-text="是" cancel-text="否" @confirm="toDelete(record)">
              <template slot="title">
                <p>确定要删除这条记录吗？</p>
              </template>
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table> -->
      <detail-form :visible="detailVisible" :model="this.model" @cancel="()=>{this.detailVisible=false}" @ok="modalOk" />
      <add-children :visible="addChildrenVisible" :model="this.model" @cancel="()=>{this.addChildrenVisible=false}" @ok="modalOk" />
      <setting :visible="settingVisible" :model="this.model" @cancel="() => {this.settingVisible=false}" @ok="modalOk"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getDepartmentsLists, deleteDepartment, putDepartment, addDepartment } from '@/api/manage'
import { STable } from '@/components'
import DetailForm from './Detail'
import { notification } from 'ant-design-vue'
import AddChildren from './addChildren'
import Setting from './Setting'
export default {
  name: 'DepartmentLists',
  data () {
    return {
      loading: false,
      addChildrenVisible: false,
      model: {},
      settingVisible: false,
      detailVisible: false,
      queryParam: {},
      departmentList: [],
      columns: [
        {
          title: '#',
          dataIndex: 'id'
        },
        {
          title: '部门名称',
          dataIndex: 'departName'
        },
        {
          title: '公司名称',
          dataIndex: 'companyName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '250px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        // 不要在这里修改分页大小，STable已经写死默认每一页10条
        const cParameter = { pageSize: parameter.pageSize, pageNum: parameter.pageNo }
        const requestParameter = Object.assign({}, cParameter, this.queryParam)
        return getDepartmentsLists(requestParameter).then(res => {
          const tempList = [...res.data]
          const rootList = []
          tempList.forEach((item, index) => {
            if (item.rootId === null) {
              rootList.push(item)
            }
          })
          rootList.forEach(item => {
            item.children = this.jsonToTree(item, res.data)
          })
          console.log(rootList)
          this.departmentList = res.data
          return {
            data: rootList,
            totalCount: parseInt(res.headers['x-count']),
            pageNo: 1,
            pageSize: rootList.length
          }
        })
      }
    }
  },
  components: {
    STable,
    DetailForm,
    AddChildren,
    Setting
  },
  created () {
    // this.init()
    console.log(deleteDepartment)
    console.log(putDepartment)
    console.log(addDepartment)
    getDepartmentsLists({ parentId: '' }).then(res => {
      const tempList = [...res.data]
      const rootList = []
      tempList.forEach((item, index) => {
        if (item.rootId === null) {
          rootList.push(item)
        }
      })
      rootList.forEach(item => {
        item.children = this.jsonToTree(item, res.data)
      })
      console.log(rootList)
      this.departmentList = res.data
      return {
        data: rootList,
        totalCount: parseInt(res.headers['x-count']),
        pageNo: 1,
        pageSize: rootList.length
      }
    })
  },
  methods: {
    init () {
      getDepartmentsLists({ parentId: '' }).then(res => {
        const tempList = [...res.data]
        const rootList = []
        tempList.forEach((item, index) => {
          if (item.rootId === null) {
            rootList.push(item)
          }
        })
        rootList.forEach(item => {
          item.children = this.jsonToTree(item, res.data)
        })
        console.log(rootList)
        this.departmentList = res.data
        return {
          data: rootList,
          totalCount: parseInt(res.headers['x-count']),
          pageNo: 1,
          pageSize: rootList.length
        }
      })
    },
    initTree (record) {
      this.loading = true
      getDepartmentsLists({ parentId: record.id }).then(res => {
        res.data.forEach(item => {
          item.children = []
        })
        record.children = res.data
        this.loading = false
      })
    },
    iconExpand (expanded, record) {
      if (expanded) {
        this.initTree(record)
      }
    },
    addChildren (record) {
      this.addChildrenVisible = true
      console.log(record)
      this.model = record
    },
    jsonToTree (item, data) {
      const childrens = []
      data.forEach(_item => {
        if (_item.parentId === item.id) {
          childrens.push(_item)
          _item.children = this.jsonToTree(_item, data)
        }
      })
      return childrens
    },
    modalOk () {
      const model = { ...this.model }
      this.initTree(model)
      this.model = {}
      this.settingVisible = false
      this.detailVisible = false
      this.addChildrenVisible = false
      // this.$refs.table.refresh(false)
    },
    // init () {
    //   getDepartmentsLists({ pageSize: this.pageSize, pageNum: this.pageNo }).then(res => {
    //     this.departmentList = res.data
    //     console.log(this.departmentList)
    //   })
    // },
    toAdd () {
      this.model = {}
      this.detailVisible = true
    },
    toSetting (record) {
      this.model = record
      this.settingVisible = true
    },
    toDetail (record) {
      this.model = record
      this.detailVisible = true
    },
    toDelete (record) {
      deleteDepartment(record.id).then(res => {
        notification.success({
          message: '提示',
          description: '删除成功'
        })
        this.init()
        // this.$refs.table.refresh(false)
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
